<template>
	<view>
		<mvBar :mysNavConfig="mysNavConfig" @onLeftClick="onLeftClick"></mvBar>
		<u-toast ref="uToast"></u-toast>
		<scroll-view scroll-y="true">
			<view style="margin-bottom: 20px;">
				<view class="uniview" v-for="(item,index) in indexinfo" :key="index">
					<uni-card padding="10px 0">
						<template v-slot:title>
							<uni-list>
								<uni-list-item v-if="item.enableStatus==1" :show-switch="true" :switchChecked="true"
									:title="item.name" @switchChange="switchchange(item.pointFeatureRealId)" />
								<uni-list-item v-else :show-switch="true" :switchChecked="false" :title="item.name"
									@switchChange="switchchange(item.pointFeatureRealId)" />
							</uni-list>
						</template>
						<view class="indexform">
							<u-form labelPosition="left" :model="item">
								<u-form-item label="最小值" label-width="75" prop="min" borderBottom>
									<u--input type="digit" v-model="item.min" placeholder="请输入最小值" border="none">
									</u--input>
								</u-form-item>
								<u-form-item label="预警值" label-width="75" prop="unusual" borderBottom>
									<u--input type="digit" v-model="item.unusual" placeholder="请输入预警值" border="none">
									</u--input>
								</u-form-item>
								<u-form-item label="报警值" label-width="75" prop="warning" borderBottom>
									<u--input type="digit" v-model="item.warning" placeholder="请输入报警值" border="none">
									</u--input>
								</u-form-item>
								<u-form-item label="基准值" label-width="75" prop="basis" borderBottom>
									<u--input type="digit" v-model="item.basis" placeholder="请输入基准值" border="none">
									</u--input>
								</u-form-item>
							</u-form>
						</view>
					</uni-card>
				</view>
				<view class="button">
					<u-button type="primary" text="确定" customStyle="margin-top: 10px" @click="submit"></u-button>
				</view>
				<view class="button">
					<u-button type="success" text="同步" customStyle="margin-top: 10px" @click="syncinfo"></u-button>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				mysNavConfig: {
					/* 占位开启  */
					navPadding: true,
					/* 背景 */
					bgColor: "#0055ff",
					/* 标题 （屏幕中心居中 两边图标中心居中使用slot center1） */
					navTitle: {
						text: "指标管理",
						color: "#ffffff",
						fontSize: "18px", // px upx rpx
						fontWeight: "500", // 100 - 700
					},
					btnType: "type2",
					isCustomFn: true,
					type2Config: {
						// 左图标
						leftPath: "/static/img/back.png",
						// 右图标
						rightPath: "/static/img/home.png",
						// 圆角
						radius: "40rpx",
					}
				},
				equipmentRealId: "",
				pointRealId: '',
				userInfo: '',
				indexinfo: [],
			};
		},
		onLoad(value) {
			this.equipmentRealId = value.equipmentRealId;
			this.pointRealId = value.pointRealId;
			this.userInfo = uni.getStorageSync('userInfo');
			this.getindex()
		},
		methods: {
			//同步
			syncinfo() {
				//点击以后出现弹窗
				uni.showLoading({
					title: "同步中...",
					mask: true,
				});
				uni.request({
					url: getApp().globalData.serverAddress + this.$uri.Url_coincidentIndicator.replace(
						"{userRealId}", this.userInfo.userRealId).replace(
						"{equipmentRealId}", this.equipmentRealId).replace(
						"{pointRealId}", this.pointRealId),
					method: "POST",
					success: (res) => {
						if (res.data.code == 0) {
							this.indexinfo = res.data.data
							this.$utils.showToast(this, '同步成功！', 'success');
						} else {
							this.$utils.showToast(this, res.data.msg, 'error');
						}
					},
					fail: (res) => {
						this.$utils.showToast(this, res.data.msg, 'error');
					},
					complete: () => {
						uni.hideLoading();
					}
				})
			},
			// 修改
			submit() {
				let alterlist = []
				for (let va of this.indexinfo) {
					if (va.min !== '' && va.unusual !== '' && va.warning !== '' && va.basis !== '') {
						if (va.min < va.unusual && va.unusual < va.warning && va.basis >= 0) {
							alterlist.push({
								PointFeatureRealId: va.pointFeatureRealId,
								EnableStatus: va.enableStatus,
								Min: va.min,
								Unusual: va.unusual,
								Warning: va.warning,
								Basis: va.basis,
							})
						} else {
							this.$utils.showToast(this, va.name + "阈值设置错误", 'error', 1, '');
						}
					} else {
						this.$utils.showToast(this, va.name + "阈值设置不能为空", 'error', 1, '');
					}
				}
				if (alterlist.length == this.indexinfo.length) {
					uni.request({
						url: getApp().globalData.serverAddress + this.$uri.Url_alterthresholdvalue.replace(
							"{userRealId}", this.userInfo.userRealId).replace(
							"{equipmentRealId}", this.equipmentRealId).replace(
							"{pointRealId}", this.pointRealId),
						data: alterlist,
						dataType: "json",
						method: "POST",
						success: (res) => {
							if (res.data.code == 0) {
								this.$utils.showToast(this, '修改成功！', 'success', 1,
									'/pages/equipments/pointList?equipmentRealId=' + this
									.equipmentRealId);
							} else {
								this.$utils.showToast(this, res.data.msg, 'error', 1, '');
							}
						},
						fail: (res) => {
							this.$utils.showToast(this, res.data.msg, 'error', 1, '');
						}
					})
				}
			},
			switchchange(value) {
				for (let it of this.indexinfo) {
					if (value == it.pointFeatureRealId) {
						if (it.enableStatus == 1) {
							it.enableStatus = 2
						} else {
							it.enableStatus = 1
						}
					}
				}
			},
			// 获取指标信息
			getindex() {
				uni.request({
					url: getApp().globalData.serverAddress + this.$uri.Uri_getPointInfo.replace(
							"{userRealId}",
							this
							.userInfo.userRealId).replace("{equipmentRealId}", this
							.equipmentRealId).replace("{pointRealId}", this
							.pointRealId)
						.replace("{equipmentRealId}", this.equipmentRealId),
					dataType: "json",
					method: "GET",
					success: (res) => {
						this.indexinfo = res.data.data.pointFeatures
					}
				})
			},
			// 返回到测点列表
			onLeftClick() {
				uni.redirectTo({
					url: '/pages/equipments/pointList?equipmentRealId=' + this.equipmentRealId
				})
			},
		},
	};
</script>

<style lang="scss" scoped>
	.indexform {
		padding: 0px 10px;
	}

	.button {
		margin: 0px 5%;
		width: 40%;
		display: inline-block;
	}

	.uniview {
		margin: 20px;
		padding: 0px 10px;
		background-color: #ffffff;
		box-shadow: 0px 0px 3px 1px rgba(212, 214, 220, 1.0);
		border-radius: 5%;
	}
</style>
